package com.example.q_kang.hhh.order;


import com.bulong.rudeness.RudenessScreenHelper;
import com.example.q_kang.hhh.R;
import com.example.q_kang.hhh.order.match.BuyConfirmActivity;
import com.example.q_kang.hhh.order.match.BuyDakuanMatchDetailActivity;
import com.example.q_kang.hhh.order.match.SellCompleteActivity;
import com.example.q_kang.hhh.order.match.SellConfirmActivity;
import com.example.q_kang.hhh.order.match.SellDakuanDetailActivity;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.base.MVPActivity;
import com.example.q_kang.hhh.order.MatchListPresenter;
import com.sskj.base.adapter.slimadapter.IViewInjector;
import com.sskj.base.adapter.slimadapter.SlimAdapter;
import com.sskj.base.adapter.slimadapter.SlimInjector;
import com.sskj.base.api.Constant;
import com.sskj.base.base.App;
import com.sskj.base.bean.BuyPiPeiListBean;
import com.sskj.base.bean.PiPeiStatusListBean;
import com.sskj.base.mvchelper.ModelRx2DataSource;
import com.sskj.base.utils.DateUtil;
import com.sskj.base.view.DividerLineItemDecoration;
import com.sskj.base.view.ToolBarLayout;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

/**
 * @author Hey
 * Create at  2019/04/13
 */
public class MatchListActivity extends MVPActivity<MatchListPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private int type;
    private String order;
    private String order_type;

    private SlimAdapter<PiPeiStatusListBean.PiPeiStatusBean> slimAdapter;
    private MVCCoolHelper<List<PiPeiStatusListBean.PiPeiStatusBean>> mvcHelper;
    private ModelRx2DataSource<PiPeiStatusListBean.PiPeiStatusBean> dataSource;

    @Override
    public int getLayoutId() {
        return R.layout.activity_match_list;
    }

    @Override
    public MatchListPresenter getPresenter() {
        return new MatchListPresenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(view -> finish());
        type = getIntent().getIntExtra("type", -1);
        order = getIntent().getStringExtra("order");
        order_type = getIntent().getStringExtra("order_type");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 20))
                .setDividerColor(ContextCompat.getColor(this, R.color.transparent)));
        slimAdapter = SlimAdapter.create().register(R.layout.item_buy_peidui, new SlimInjector<PiPeiStatusListBean.PiPeiStatusBean>() {
            @Override
            public void onInject(PiPeiStatusListBean.PiPeiStatusBean data, IViewInjector injector) {
                injector.clicked(R.id.btn_check, view -> {
                    switch (type) {
                        case Constant.BUY:
                            if (data.getIs_type().equals("1")) {
                                BuyDakuanMatchDetailActivity.start(App.INSTANCE, data.getId());
                            } else if (data.getIs_type().equals("2")) {
                                BuyConfirmActivity.start(App.INSTANCE, data.getId(), "0");
                            } else if (Integer.valueOf(data.getIs_type()) >= 6) {
                                BuyConfirmActivity.start(App.INSTANCE, data.getId(), "1");
                            }
                            break;
                        case Constant.SELL:
                            if (data.getIs_type().equals("1")) {
                                SellDakuanDetailActivity.start(App.INSTANCE, data.getId());
                            } else if (data.getIs_type().equals("2")) {
                                SellConfirmActivity.start(App.INSTANCE, data.getId());
                            } else if (Integer.valueOf(data.getIs_type()) >= 6) {
                                SellCompleteActivity.start(App.INSTANCE, data.getId());
                            }
                            break;
                        case Constant.ALL:
                            BuyConfirmActivity.start(App.INSTANCE, data.getId(), "1");
                            break;
                    }
                }).text(R.id.tv_money, data.getPd_price())
                        .text(R.id.tv_date, DateUtil.getDateFormat(Long.valueOf(data.getAdd_time()) * 1000, DateUtil.PATTERN_J));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        if (type == Constant.BUY) {
            dataSource = new ModelRx2DataSource<>(
                    (ModelRx2DataSource.OnLoadSource<PiPeiStatusListBean.PiPeiStatusBean>)
                            page -> mPresenter.getBuyPiPeiStatus(page, order), 10);
        } else if (type == Constant.SELL) {
            dataSource = new ModelRx2DataSource<>(
                    (ModelRx2DataSource.OnLoadSource<PiPeiStatusListBean.PiPeiStatusBean>)
                            page -> mPresenter.getSellPiPeiStatus(page, order), 10);
        } else if (type == Constant.ALL) {
            dataSource = new ModelRx2DataSource<>(
                    (ModelRx2DataSource.OnLoadSource<PiPeiStatusListBean.PiPeiStatusBean>)
                            page -> mPresenter.getBuyCompleteStatus(page, order), 10);
        }
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    @Override
    public void initData() {

    }

    public static void start(Context context, int type, String order, String order_type) {
        Intent intent = new Intent(context, MatchListActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("order", order);
        intent.putExtra("order_type", order_type);
        context.startActivity(intent);
    }

}
